
import { _decorator, Component, macro, input, Node, RigidBody2D, v2, Input, EventKeyboard, KeyCode, v3, Animation } from 'cc';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = heroManager
 * DateTime = Tue Aug 29 2023 21:33:08 GMT+0800 (中国标准时间)
 * Author = zshzshzsh15
 * FileBasename = heroManager.ts
 * FileBasenameNoExtension = heroManager
 * URL = db://assets/Script/heroManager.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */




const put = {}
const State = {
    stand: 1,
    attack: 2,
}

//状态机，修改状态，再通过状态的改变

@ccclass('heroManager')
export class heroManager extends Component {
    _speed = 0
    private sp
    private heroState
    private lv
    private heroAnimation
    private anima
    combo: number;
    protected onLoad(): void {
        this._speed = 10
        this.sp = v2(0, 0)
        this.heroState = State.stand
        this.anima = 'idle'
        this.combo = 0  //连击数
        this.heroAnimation = this.node.getComponent(Animation)
        this.heroAnimation.on(Animation.EventType.FINISHED, this.back, this)

        input.on(Input.EventType.KEY_DOWN, this.down, this)
        input.on(Input.EventType.KEY_UP, this.up, this)

    }

    back(e, data) {
        if (data.name == 'attack' || data.name == 'attack2' || data.name == 'attack3') {
            this.heroState = State.stand
            this.combo = (this.combo + 1) % 3
            this.setAni("idle")
            setTimeout(() => {
                if(this.heroState===State.attack){
                    return
                }
                this.combo=0
            }, 500);
        }
    }

    onDestroy() {
        input.off(Input.EventType.KEY_DOWN, this.down, this)
        input.off(Input.EventType.KEY_UP, this.up, this)
    }
    down(e) {
        put[e.keyCode] = 1
    }
    up(e) {

        put[e.keyCode] = 0
    }
    start() {

    }

    setAni(anim) {
        if (this.anima === anim) return;
        this.anima = anim
        this.heroAnimation.play(this.anima)
    }

    update(deltaTime: number) {
        let anima = this.anima
        this.lv = this.node.getComponent(RigidBody2D).linearVelocity
        let scaleX = Math.abs(this.node.scale.x)


        switch (this.heroState) {
            case State.stand: {
                if (put[macro.KEY.j]) {
                    this.heroState = State.attack
                }
                break;

            }

            // case State.attack:{
            //     if (put[macro.KEY.j]) {
            //         if (this.combo == 0) {
            //             anima = 'attack'
            //         } else if (this.combo == 1) {
            //             anima = 'attack2'
            //         } else if (this.combo == 2) {
            //             anima = 'attack3'
            //         }
    
    
            //     }
            // }


        }


        if (this.heroState == State.attack) {
            if (put[macro.KEY.j]) {
                if (this.combo == 0) {
                    anima = 'attack'
                } else if (this.combo == 1) {
                    anima = 'attack2'
                } else if (this.combo == 2) {
                    anima = 'attack3'
                }


            }

        }

        if (this.heroState != State.stand) {
            this.sp.x = 0
        } else {
            if (put[macro.KEY.a] || put[macro.KEY.left]) {
                this.sp.x = -1
                this.node.scale = v3(-scaleX, 2, 1)
                anima = 'run'
            } else if (put[macro.KEY.d] || put[macro.KEY.right]) {
                this.sp.x = 1

                this.node.scale = v3(scaleX, 2, 1)
                anima = 'run'
            } else {
                this.sp.x = 0
                anima = 'idle'
            }
        }

        if (this.sp.x) {
            this.lv.x = this.sp.x * this._speed
        } else {
            this.lv.x = 0
        }


        this.node.getComponent(RigidBody2D).linearVelocity = this.lv;
        // [4]
        if (anima) {
            this.setAni(anima)
        }
        console.log(this.anima)


    }


}